<?php
session_start();
if(isset($_SESSION['admin_id'])) {
    include('../include/dbFunctions.php');
    $_SESSION['mes']="";
//Innitialization of variable
    $table = 'program';

//Retrieve data from POST
    $program_name = htmlspecialchars($_POST['program_name']);
    $program_description = nl2br(htmlspecialchars($_POST['program_description']));
    $program_lesson_duration = htmlspecialchars($_POST['program_lesson_duration']);
    $program_support = htmlspecialchars($_POST['program_support']);
    $last_modified = date("Y-m-d H:i:s");
    $program_status = "active";
    if (isset($_POST['education'])) {
        $education_level = $_POST['education'];
    } else {
        $education_level = Array(0,1,2,3,4);
    }
//Add data to database
    $sQuery = 'INSERT INTO program(program_name,program_description,program_lesson_duration,program_support,program_status,last_modified,created_date)
VALUES ("'.$program_name.'","'.$program_description.'","'.$program_lesson_duration.'","'.$program_support.'","'.$program_status.'","'.$last_modified.'","'.$last_modified.'")';
    $sResult = executeQuery($sQuery);

    $rQuery = 'SELECT program_id from program ORDER BY program_id DESC LIMIT 1';
    $rResult = executeSelectQuery($rQuery);
    $program_id=$rResult[0]['program_id'];


    $arr_num=count($education_level);
    $i=0;
    while ($i < $arr_num) {
        $aQuery = "INSERT INTO program_education(education_id,program_id) values('$education_level[$i]','$program_id')";
        $aResult = executeQuery($aQuery);
        $i++;
    }

    if ($rResult!=0&&$rResult!=0&&$aResult!=0) {
        $_SESSION['mes'] = "Your record has been added successfully.";
    } else {
        $_SESSION['mes'] = "Error! Please try again";
    }
    header('Location: programManage.php');
}else {
    header("Location: ../include/login.html");
}
?>